<?php
/**
 * quick_curd
 * ============================================================================
 * 版权所有 2024-2099 wltx666.com，并保留所有权利。
 * 网站: https://wltx666.com
 * ============================================================================
 * Author: 草上飞
 * Email: wltx@alipay168.cn
 * QQ: 2719875001
 * Date: 2025/08/07
 * 描述: api_xxx 代表前端接口,其它代表后端接口
 */

namespace app\result\auto;

use app\model\auto\UploadFilesLog;
use app\result\BaseResult;
use app\result\ToolsResult;


class UploadFilesLogResult extends BaseResult
{
    public function __construct()
    {
        parent::__construct();
        $this->model = new UploadFilesLog();
    }

    //列表页面配置
    public function list_config(): array
    {
        return $this->success_return('success', [
            'form_fields' => method_exists($this, 'form_fields') ? $this->form_fields() : [],
            'el_table_column' => $this->el_table_column(),
            'search_fields' => $this->_searchField()
        ]);
    }

    // 表单字段
    public function form_fields(): array
    {
        return [
        ];
    }

    // 表格列
    public function el_table_column(): array
    {
        return [
            ['prop' => 'name', 'type' => 'text', 'label' => '文件名称', 'minWidth' => '120', 'align' => 'center',],
            ['prop' => 'size', 'type' => 'text', 'label' => '大小', 'minWidth' => '120', 'align' => 'center',],
            ['prop' => 'type', 'type' => 'text', 'label' => '类型', 'minWidth' => '120', 'align' => 'center',],
            ['prop' => 'upload_type', 'type' => 'tags', 'label' => '存储方式', 'minWidth' => '120', 'align' => 'center',
                'tags' => [['label' => '本地', 'value' => 'local', 'color' => '', 'type' => 'success',], ['label' => 'oss', 'value' => 'oss', 'color' => '', 'type' => 'success',], ['label' => '七牛云', 'value' => 'qiniu', 'color' => '', 'type' => 'success',],]],
            ['prop' => 'path', 'type' => 'text', 'label' => '保存路径', 'minWidth' => '120', 'align' => 'center',],
            ['prop' => 'full_url', 'type' => 'text', 'label' => '完整路径', 'minWidth' => '120', 'align' => 'center',],
            ['prop' => 'created_at', 'type' => 'text', 'label' => '上传时间', 'minWidth' => '120', 'align' => 'center',],
            ['prop' => 'user_type', 'type' => 'tags', 'label' => '来源', 'minWidth' => '120', 'align' => 'center',
                'tags' => [['label' => '后台', 'value' => '1', 'color' => '', 'type' => 'success',], ['label' => '用户', 'value' => '2', 'color' => '', 'type' => 'success',],]],
            ['prop' => 'md5', 'type' => 'text', 'label' => 'md5', 'minWidth' => '120', 'align' => 'center',],
            ['prop' => 'user_id', 'type' => 'text', 'label' => '上传人ID', 'minWidth' => '120', 'align' => 'center',],
            ['prop' => 'to_table', 'type' => 'text', 'label' => '关联的表', 'minWidth' => '120', 'align' => 'center',],
        ];
    }

    //列表搜索字段
    private function _searchField(): array
    {
        return [
            ['name' => 'name', 'label' => '文件名称', 'type' => 'input', 'placeholder' => '文件名称',],
            ['name' => 'upload_type', 'label' => '存储方式', 'type' => 'select', 'placeholder' => '存储方式',
                'options' => [['label' => '本地', 'value' => 'local',], ['label' => 'oss', 'value' => 'oss',], ['label' => '七牛云', 'value' => 'qiniu',],]],
        ];
    }

    // 获取公共条件限制
    private function _where($args = []): array
    {
        $where = [];

        if (!empty($args['name'])) {
            $where[] = ['name', '=', $args['name']];
        }
        if (!empty($args['upload_type'])) {
            $where[] = ['upload_type', '=', $args['upload_type']];
        }
        return $where;
    }

    public function list($args = []): array
    {
        $page = intval($args['page'] ?? 1);
        $pageSize = intval($args['limit'] ?? 10);
        $sort_by = trim($args['sort_by'] ?? 'id desc');
        $where = $this->_where($args);
        $total = $this->model->data_count($where);
        $data = $this->model->data_list($where, '*', $sort_by, $page, $pageSize);
        if ($data) {
            foreach ($data as &$v) {
                $v['size'] = ToolsResult::format_bytes($v['size']);
            }
            unset($v);
        }
        return $this->success_return('success', [
            'total' => $total,
            'list' => $data,
        ]);
    }


}
